<template>
<Modal
	v-model="show"
	title="信息补充"
	:width="1000"
	>
	<div>
		<Form :model="info" :label-width="150" style="width: 100%" :rules="ruleValidate" ref="form">
			<Row>
				<Col span="12">
					<FormItem label="固定资产编号：" prop="assets_no">
						<Input v-model="info.assets_no" placeholder="" style="width:260px;"></Input>
					</FormItem>
				</Col>
				<Col span="12">
					<FormItem label="累计折旧余额：" prop="depreciation">
						<Input :value="info.depreciation" placeholder=""style="width:260px;"></Input>
					</FormItem>
				</Col>
			</Row>
			<Row>
				<Col span="12">
					<FormItem label="使用人：" prop="user">
						<Input v-model="info.user" placeholder="" style="width:260px;"></Input>
					</FormItem>
					<FormItem label="固定资产名称：" prop="name">
						<Input v-model="info.name" placeholder="" style="width:260px;"></Input>
					</FormItem>
				</Col>
				<Col span="12">
					<FormItem label="使用年限：">
						<!--<span>{{info.useyear}}年</span>
						<span>{{info.usemonth}}月</span>-->
						<InputNumber :max="10" :min="0" v-model="info.useyear" :readonly="readyear" @on-change="endyear_change" style="width:60px;"></InputNumber>年
						<InputNumber :max="12" :min="0" v-model="info.usemonth" :readonly="readmonth" @on-change="endmonth_change" style="width:60px;"></InputNumber>月
					</FormItem>
				</Col>
			</Row>
			<Row>
				<Col span="12">
					<FormItem label="单位：" prop="num">
						<Input v-model="info.num" placeholder="" style="width:260px;"></Input>
					</FormItem>
				</Col>
				<Col span="12">
					<FormItem label="使用状态：" prop="status">
						<Select v-model="info.status" style="width:260px;">
							<Option value="0" >在用</Option>
							<Option value="1" >报废</Option>
							<Option value="2" >折旧完成</Option>
						</Select>
					</FormItem>
				</Col>
			</Row>
			<Row>
				<Col span="12">
					<FormItem label="取得日期：" prop="start_date">
						<DatePicker type="date" v-model="info.start_date"  placeholder="选择日期" style="width:260px;" @on-change="start_month_change"></DatePicker>
					</FormItem>
					<FormItem label="截止日期：" prop="val">
						<DatePicker type="date" v-model="info.end_date" :readonly="readend" placeholder="选择日期" style="width:260px;" @on-change="end_month_change"></DatePicker>
				</FormItem>
				</Col>
			</Row>
		</Form>
	</div>
	<div slot="footer" style="text-align: center;">
		<Button type="success" size="large"  @click="handleSubmit('form')">提交</Button>
	</div>
</Modal>
</template>

<script>

export default {
  name: 'user_bar',
  data(){
	return {
		show:false,
		def_info:{
			assets_no:'',
			depreciation:'',
			user:'',
			name:'',
			useyear:0,
			usemonth:0,
			num:'',
			status:'0',
			start_date:'',
			end_date:'',
			id:0
		},
		info:{
			assets_no:'',
			depreciation:'',
			user:'',
			name:'',
			useyear:0,
			usemonth:0,
			num:'',
			status:'0',
			start_date:'',
			end_date:'',
			id:0
		},
		ruleValidate:{
			assets_no:[
				{ required: true, message: '固定资产编号不能为空', trigger: 'blur' }
			],
			user:[
				{ required: true, message: '使用人不能为空', trigger: 'blur' }
			],
			name:[
				{ required: true, message: '固定资产名称不能为空', trigger: 'blur' }
			],
			num:[
				{ required: true, message: '单位不能为空', trigger: 'blur' }
			],
			start_date:[
				{ required: true, message: '请选择取得日期', trigger: 'blur' }
			],
		},
		readyear: false,
		readmonth: false,
		readend: false,
	};
  },
  props:['value','cinfo'],
  created(){
	this.show = this.value;
  },
  mounted () {
  },
  watch:{
	value(val){
		this.show = val;
		
		if(this.cinfo){
			this.cinfo.useyear = parseInt(this.cinfo.useyear);
			this.cinfo.usemonth = parseInt(this.cinfo.usemonth);
			
			for(var i in this.info){
				if(typeof(this.cinfo[i])!='undefined'){
					this.info[i] = this.cinfo[i];
				}
				else{
					this.info[i] = this.def_info[i];
				}
			}
		}
	},
	show(val){
		this.$emit('input',val);
	}
  },
  methods: {
	start_month_change(date){
		this.info.start_date =  date;
	},
	end_month_change(date){
		this.info.end_date =  date;
		if(date){
			this.readyear = true;
			this.readmonth = true;
		}else{
			this.readyear = false;
			this.readmonth = false;
		}
		

		//在这里更改使用年限
		if(this.info.start_date){
			var year_month = this.getDateTime(this.info.start_date,date);
			this.info.useyear = parseInt(year_month['year']);
			this.info.usemonth = parseInt(year_month['month']);
		}
	},
	endyear_change(date){
		this.info.useyear =  date;
		if(date > 0){
			this.readend = true;
		}else{
			this.readend = false;
		}

		//自动更改截止日期
		if(this.info.start_date){
			this.info.end_date = this.getEndDate(this.info.start_date,date,this.info.usemonth);
		}
	},
	endmonth_change(date){
		this.info.usemonth =  date;
		if(date > 0){
			this.readend = true;
		}else{
			this.readend = false;
		}

		//自动更改截止日期
		if(this.info.start_date){
			this.info.end_date = this.getEndDate(this.info.start_date,this.info.useyear,date);
		}
	},
	handleSubmit(name) {
		this.info.start_date = this.formatDateTime(this.info.start_date);
		this.info.end_date = this.formatDateTime(this.info.end_date);
		
		this.$refs[name].validate((valid) => {
			if (valid) {
				this.einfo_ok();
			} 
		})
	},
	einfo_ok(){
	
		var self = this;
		var postdata = this.info;
		
		Whttp.load('Accounting/Assets/save',postdata,function(result){
			self.show = false;
			self.$Message.info('提交成功');
			self.$emit('success',result);
		});
		
	},
	//通过开始日期和截止日期获取使用年限
	getDateTime(start,end){
		start = this.formatDateTime(start);
		end = this.formatDateTime(end);

		var starts = new Array();
		var ends = new Array();
		starts = start.split('-');
		ends = end.split('-');
		var start_year = starts[0];//获取开始日期的完整年份(4位,1970-????)
		var start_month = starts[1];//获取开始日期的月份
		var start_day = starts[2];//获取开始日期的月份
		var starttime = new Date(start_year, start_month, start_day);
		var starttimes = starttime.getTime();

		var end_year = ends[0];//获取截止日期的完整年份(4位,1970-????)
		var end_month = ends[1];//获取截止日期的月份
		var end_day = ends[2];//获取截止日期的月份
		var endtime = new Date(end_year, end_month, end_day);
		var endtimes = endtime.getTime();

		if(endtimes < starttimes){
			return "截止日期不能小于开始日期!";
		}

		var res = [];
		res['year'] = end_year - start_year;
		if(start_day > end_day){//未满一个月的情况算一个月
			res['month'] = end_month - start_month;
		}else{//指满一个月多几天的情况
			res['month'] = end_month - start_month + 1;
			if(res['month'] >= 12){
				res['year'] = res['year'] + 1;
				res['month'] = res['month'] - 12;
			}else if(res['month'] < 0){
				res['year'] = res['year'] - 1;
				res['month'] = res['month'] + 12;
			}
		}

		return res;

	},
	//通过开始日期和使用年限获取截止日期
	getEndDate(val, year, month){
		val = this.formatDateTime(val);

		var n = year*12 + month;
		var str = val.split('-');
		var d = new Date(str[0], str[1], str[2]);
		// 因为getMonth()获取的月份的值只能在0~11之间所以我们在进行setMonth()之前先给其减一
		d.setMonth((d.getMonth()-1) + n);
		var yy1 = d.getFullYear();
		var mm1 = d.getMonth()+1;
		var dd1 = d.getDate()-1;
		if(dd1 == '00'){
		mm1 = parseInt(mm1)-1;
		var new_date = new Date(yy1,mm1,1);
		dd1 = (new Date(new_date.getTime()-1000*60*60*24)).getDate()
		}
		if (mm1 < 10 ) {
		mm1 = '0' + mm1;
		}
		if (dd1 < 10) {
		dd1 = '0' + dd1;
		}
		return yy1 + '-' + mm1 + '-' + dd1;

	},
	//将date格式的日期转换为Y-m-d
	formatDateTime(start_date){
		var startdate = new Date(start_date);
		var y = startdate.getFullYear();  
		var m = startdate.getMonth() + 1;  
		m = m < 10 ? ('0' + m) : m;  
		var d = startdate.getDate();  
		d = d < 10 ? ('0' + d) : d;  
		  
		return y + '-' + m + '-' + d;
	},
  }
}
</script>
<style scoped>
</style>